/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.seg.bc;

import edu.upn.sigemfi.seg.be.Departamento;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrador
 */
public class DepartamentoDelegate {

    private DepartamentoFacadeLocal local;

    public DepartamentoDelegate() {
        local = lookupDepartamentoFacade();
    }

    public Departamento buscar(String codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupDepartamentoFacade();
            throw new Exception("Buscar Departamento. " + e.getMessage(), e);
        }
    }

    public List<Departamento> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupDepartamentoFacade();
            throw new Exception("Listar Departamentos. " + e.getMessage(), e);
        }
    }

    private DepartamentoFacadeLocal lookupDepartamentoFacade() {
        try {
            Context c = new InitialContext();
            return (DepartamentoFacadeLocal) c.lookup("java:comp/env/DepartamentoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
